package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AdminAgentInfo;
import com.ruoyi.system.service.IAdminAgentInfoService;
import com.ruoyi.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.WebApplicationContextUtils;
import sun.management.Agent;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.math.BigDecimal;

/**
 * @program: ruoyi
 * @description: 初始化基本数据监听器
 * @author: Mr.sun
 * @create: 2020-11-13 15:58
 **/
public class InitBasicDataServiceImpl implements ServletContextListener {

    private static final Logger logger = LoggerFactory.getLogger(InitBasicDataServiceImpl.class);

    @Autowired
    private IAdminAgentInfoService iAdminAgentInfoService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private AdminAgentInfoServiceImpl adminAgentInfoServiceImpl;

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {

    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        /*iAdminAgentInfoService = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext()).getBean(IAdminAgentInfoService.class);
        iSysUserService = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext()).getBean(ISysUserService.class);
        adminAgentInfoServiceImpl = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext()).getBean(AdminAgentInfoServiceImpl.class);
        SysUser admin = iSysUserService.selectUserByUserName("admin");
        AdminAgentInfo adminAgentInfo = iAdminAgentInfoService.selectAdminAgentInfoByRoom(admin.getRoom());
        if (StringUtils.isBlank(admin.getRoom())){
            SysUser sysUser = new SysUser();
            String room = adminAgentInfoServiceImpl.generateRoom();
            sysUser.setRoom(room);
            sysUser.setUserId(admin.getUserId());
            iSysUserService.updateUser(sysUser);
        }
        SysUser newAdmin = iSysUserService.selectUserByUserName("admin");
        if (adminAgentInfo==null) {
            AdminAgentInfo insertAgent = new AdminAgentInfo();
            insertAgent.setRoom(newAdmin.getRoom());
            insertAgent.setAgentName("admin");
            insertAgent.setOdds(new BigDecimal(1));
            iAdminAgentInfoService.insertAdminAgentInfo(insertAgent);
        }*/

    }
}
